package cn.itsub.proxy.client.config;

import cn.itsub.proxy.common.Config;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 夏增明
 * @Date: 2021/11/02/9:44
 * @Description:
 */
@Slf4j
public class ClientProperties {

    private static ClientProperties instance=null;
    private static Config config = null;

    @Getter private String clientKey;   //客户端凭据
    @Getter private boolean sslEnable;  //是否启用SSL加密连接
    @Getter private String sslJksPath;  //SSL证书路径
    @Getter private String sslKeyStorePassword;  //SSL证书路径
    @Getter private String serverHost;  //服务器地址或域名
    @Getter private int serverPort;  //服务器端口




    public static ClientProperties getInstance() {
        if (config == null) {
            synchronized (Config.class) {
                if (config == null) {
                    config = Config.getInstance();
                }
            }
        }
        if (instance == null) {
            synchronized (ClientProperties.class) {
                if (instance == null) {
                    instance = new ClientProperties();
                    instance.clientKey = config.getStringValue("client.key");
                    instance.serverHost = config.getStringValue("server.host");
                    instance.serverPort = config.getIntValue("server.port");
                    instance.sslEnable = config.getBooleanValue("ssl.enable");
                    instance.sslJksPath = config.getStringValue("ssl.jksPath");
                    instance.sslKeyStorePassword = config.getStringValue("ssl.keyStorePassword");
                    //缩写属性
                    if (config.hasKey("k")) {
                        log.debug("缩写属性[k={}]", config.getStringValue("k"));
                        instance.clientKey = config.getStringValue("k");
                    }
                    if (config.hasKey("h")) {
                        log.debug("缩写属性[h={}]", config.getStringValue("h"));
                        instance.serverHost = config.getStringValue("h");
                    }
                    if (config.hasKey("p")) {
                        log.debug("缩写属性[p={}]", config.getIntValue("p"));
                        instance.serverPort = config.getIntValue("p");
                    }
                    if (config.hasKey("ssl")) {
                        log.debug("缩写属性[ssl={}]", config.getBooleanValue("ssl"));
                        instance.sslEnable = config.getBooleanValue("ssl");
                    }

                }
            }
        }

        return instance;
    }
}
